package org.eoti.ai.genetic.gp;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class Program<DATA, FITNESS>
{
	private Node<DATA,FITNESS> rootNode;
	private Criteria<DATA,FITNESS> criteria;

	public Program(Criteria<DATA,FITNESS> criteria)
	{
		this.criteria = criteria;
	}

	public String toString()
	{
		return rootNode.toString();
	}

	public void randomize(Criteria<DATA, FITNESS> criteria)
	{
		while(rootNode == null || rootNode.totalSize() < criteria.getMinProgramSize() || rootNode.totalSize() > criteria.getMaxProgramSize())
		{
			rootNode = criteria.getRandomFunction();
			if(!rootNode.isPopulated())
				rootNode.autogenerate( criteria );
		}

		setRootNode(rootNode);
	}

	protected Program<DATA, FITNESS> copy()
	{
		Program<DATA,FITNESS> program = new Program<DATA,FITNESS>(criteria);
		program.rootNode = ( Node<DATA,FITNESS> ) rootNode.copy();
		return program;
	}

	public DATA execute() throws ProgramException
	{
		return rootNode.execute();
	}

	public void setRootNode( Node<DATA,FITNESS> rootNode ){
		this.rootNode = rootNode;
	}

	public Node<DATA,FITNESS> getRootNode()
	{
		return rootNode;
	}

	/**
	 * Indicates whether some other object is "equal to" this one.
	 * <p/>
	 * The <code>equals</code> method implements an equivalence relation on non-null object references: <ul> <li>It is
	 * <i>reflexive</i>: for any non-null reference value <code>x</code>, <code>x.equals(x)</code> should return
	 * <code>true</code>. <li>It is <i>symmetric</i>: for any non-null reference values <code>x</code> and <code>y</code>,
	 * <code>x.equals(y)</code> should return <code>true</code> if and only if <code>y.equals(x)</code> returns
	 * <code>true</code>. <li>It is <i>transitive</i>: for any non-null reference values <code>x</code>, <code>y</code>,
	 * and <code>z</code>, if <code>x.equals(y)</code> returns <code>true</code> and <code>y.equals(z)</code> returns
	 * <code>true</code>, then <code>x.equals(z)</code> should return <code>true</code>. <li>It is <i>consistent</i>: for
	 * any non-null reference values <code>x</code> and <code>y</code>, multiple invocations of <tt>x.equals(y)</tt>
	 * consistently return <code>true</code> or consistently return <code>false</code>, provided no information used in
	 * <code>equals</code> comparisons on the objects is modified. <li>For any non-null reference value <code>x</code>,
	 * <code>x.equals(null)</code> should return <code>false</code>. </ul>
	 * <p/>
	 * The <tt>equals</tt> method for class <code>Object</code> implements the most discriminating possible equivalence
	 * relation on objects; that is, for any non-null reference values <code>x</code> and <code>y</code>, this method
	 * returns <code>true</code> if and only if <code>x</code> and <code>y</code> refer to the same object (<code>x ==
	 * y</code> has the value <code>true</code>).
	 * <p/>
	 * Note that it is generally necessary to override the <tt>hashCode</tt> method whenever this method is overridden, so
	 * as to maintain the general contract for the <tt>hashCode</tt> method, which states that equal objects must have
	 * equal hash codes.
	 *
	 * @param obj the reference object with which to compare.
	 *
	 * @return <code>true</code> if this object is the same as the obj argument; <code>false</code> otherwise.
	 *
	 * @see #hashCode()
	 * @see java.util.Hashtable
	 */
	@Override public boolean equals( Object obj ){
		if(obj == null) return false;
		return toString().equals(obj.toString());
	}
}
